package com.demo.apparel.mapper.web;

import com.demo.apparel.dto.ClothesAdminDTO;
import com.demo.apparel.entity.Clothes;
import com.demo.apparel.vo.ClothesSimpleVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ClothesAdminMapper {


    /**
     *
     * @param category
     * @return
     */
    @Select("SELECT DISTINCT name FROM clothes WHERE category = #{category}")
    List<String> selectSubNames(@Param("category") int category);

    @Select("""
        SELECT id, description, image_url AS image
        FROM clothes
        WHERE category = #{category} AND name = #{name}
    """)
    List<ClothesSimpleVO> selectClothesByCategoryAndName(@Param("category") int category,
                                                         @Param("name") String name);

//    @Select("""
//    <script>
//    SELECT
//        c.id, c.user_id, c.name, c.description, c.category,
//        c.image_url, c.update_time, c.status_sale, c.season,
//        u.username
//    FROM clothes c
//    LEFT JOIN user u ON c.user_id = u.id
//    <where>
//        <if test="name != null and name != ''">
//            AND c.name LIKE CONCAT('%', #{name}, '%')
//        </if>
//        <if test="category != null and category != ''">
//            AND c.category = #{category}
//        </if>
//    </where>
//    ORDER BY c.update_time DESC
//    LIMIT #{pageSize} OFFSET #{offset}
//    </script>
//""")
//    List<ClothesAdminDTO> selectClothesPage(
//            @Param("name") String name,
//            @Param("category") String category,
//            @Param("offset") int offset,
//            @Param("pageSize") int pageSize
//    );
//
//    @Select("""
//    <script>
//    SELECT COUNT(*)
//    FROM clothes
//    <where>
//        <if test="name != null and name != ''">
//            AND name LIKE CONCAT('%', #{name}, '%')
//        </if>
//        <if test="category != null and category != ''">
//            AND category = #{category}
//        </if>
//    </where>
//    </script>
//""")
//    long countClothes(@Param("name") String name, @Param("category") String category);

    @Select("""
    <script>
    SELECT
        c.id, c.user_id, c.name, c.description, c.category,
        c.image_url, c.update_time, c.status_sale, c.season,
        u.username
    FROM clothes c
    LEFT JOIN user u ON c.user_id = u.id
    <where>
        <if test="name != null and name != ''">
            AND c.name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="category != null and category != ''">
            AND c.category = #{category}
        </if>
        <if test="username != null and username != ''">
            AND u.username LIKE CONCAT('%', #{username}, '%')
        </if>
    </where>
    ORDER BY c.update_time DESC
    LIMIT #{pageSize} OFFSET #{offset}
    </script>
""")
    List<ClothesAdminDTO> selectClothesPage(
            @Param("name") String name,
            @Param("category") String category,
            @Param("username") String username,
            @Param("offset") int offset,
            @Param("pageSize") int pageSize
    );

    @Select("""
    <script>
    SELECT COUNT(*)
    FROM clothes c
    LEFT JOIN user u ON c.user_id = u.id
    <where>
        <if test="name != null and name != ''">
            AND c.name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="category != null and category != ''">
            AND c.category = #{category}
        </if>
        <if test="username != null and username != ''">
            AND u.username LIKE CONCAT('%', #{username}, '%')
        </if>
    </where>
    </script>
""")
    long countClothes(@Param("name") String name,
                      @Param("category") String category,
                      @Param("username") String username);

}
